package com.project.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.UserBean;
import com.project.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("user")
public class UserController {

    @Resource
    private UserService userService;


    //登录
    @RequestMapping("login")
    @ResponseBody
    public UserBean login(String userName, String userPwd) {
        UserBean userBean = userService.login(userName, userPwd);
        if (userBean != null) {
            return userBean;
        }
        return new UserBean();
    }


    //展示所有用户信息
    @RequestMapping("findAllUser")
    @ResponseBody
    public IPage<UserBean> findAllUser(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                                       @RequestParam(value = "pageSize", defaultValue = "3") int pageSize) {
        return userService.findAllUser(pageNo, pageSize);
    }

    //添加用户
    @RequestMapping("/addUser")
    @ResponseBody
    public String addUser(UserBean userBean) {
        userService.addUser(userBean);
        return "ok";
    }


    //删除用户
    @RequestMapping("/delUser")
    @ResponseBody
    public String delUser(int userId) {
        userService.delUser(userId);
        return "ok";
    }


    //修改用户
    @RequestMapping("/updateUser")
    @ResponseBody
    public String updateUser(int userId, String userPwd, String userGrade) {
        userService.updateUser(userId, userPwd, userGrade);
        return "ok";
    }


    //模糊查询
    @RequestMapping("/findGrade")
    @ResponseBody
    public IPage<UserBean> findGrade(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                                     @RequestParam(value = "pageSize", defaultValue = "3") int pageSize,
                                     String userGrade) {
        return userService.findGrade(pageNo, pageSize, userGrade);
    }

    @RequestMapping("/findId")
    @ResponseBody
    public UserBean findId(int userId){
        return userService.findId(userId);
    }


}
